#include "TextBlock.h"
//#include "BSUtility.h"
#include "base/Utility.h"
#define SetAttr(AttrName) SetAttribute(AttrName, ele->Attribute(AttrName));

void TextBlock::ParseXmlElement(class TiXmlElement* ele)
{
	SetAttr("pos")
		SetAttr("size")
		SetAttr("text")
}
bool TextBlock::SetAttribute(const char* attrName, const char* attrVal)
{
	if (attrVal == nullptr) return false;
	if (Control::SetAttribute(attrName, attrVal)) {
		return true;
	}
	if (strcmp(attrName, "pos") == 0)
	{
		int x, y;
		sscanf_s(attrVal, "%d,%d", &x, &y);
		pos.x = x;
		pos.y = y;
		return true;
	}
	else   if (strcmp(attrName, "size") == 0)
	{
		int x, y;
		sscanf_s(attrVal, "%d,%d", &x, &y);
		size.x = x;
		size.y = y;
		return true;
	}
	else   if (strcmp(attrName, "text") == 0)
	{
		text.val = attrVal;
		return true;
	}
	else   if (strcmp(attrName, "textcolor") == 0)
	{
		sClrText = attrVal;
		int x, y, z;
		sscanf_s(attrVal, "%d,%d,%d", &x, &y, &z);
		clrText.x = x;
		clrText.y = y;
		clrText.z = z;
		return true;
	}
	return false;
}
void TextBlock::OnDraw() {
	std::wstring wstr = Utility::Utf82Unicode(text.val);
	TrueTypeFont* ttf = TrueTypeFont::GetInst();
	glColor4f(clrText.x.val / 255., clrText.y.val / 255., clrText.z.val / 255., 1);
	ttf->drawText(const_cast<wchar_t*>(wstr.data()), pos.x.val, pos.y.val + size.y.val, zIndex.val, size.x.val, size.y.val);
}

void TextBlock::OnMouseUp(tstring name, Control* ctrl, int x, int y)
{
	onMouseUp.Invoke(this, text);
	return;
}

BlueprintScriptLibrary::Class* TextBlock::GetClass()
{
	return nullptr;
}

